home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / webmastertools / service.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  20.9 KB  |  401 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''GWebmasterToolsService extends the GDataService to streamline
  5. Google Webmaster Tools operations.
  6.  
  7.   GWebmasterToolsService: Provides methods to query feeds and manipulate items.
  8.                           Extends GDataService.
  9. '''
  10. __author__ = 'livibetter (Yu-Jie Lin)'
  11. import urllib
  12. import gdata
  13. import atom.service as atom
  14. import gdata.service as gdata
  15. import gdata.webmastertools as webmastertools
  16. import atom
  17. FEED_BASE = 'https://www.google.com/webmasters/tools/feeds/'
  18. SITES_FEED = FEED_BASE + 'sites/'
  19. SITE_TEMPLATE = SITES_FEED + '%s'
  20. SITEMAPS_FEED_TEMPLATE = FEED_BASE + '%(site_id)s/sitemaps/'
  21. SITEMAP_TEMPLATE = SITEMAPS_FEED_TEMPLATE + '%(sitemap_id)s'
  22.  
  23. class Error(Exception):
  24.     pass
  25.  
  26.  
  27. class RequestError(Error):
  28.     pass
  29.  
  30.  
  31. class GWebmasterToolsService(gdata.service.GDataService):
  32.     '''Client for the Google Webmaster Tools service.'''
  33.     
  34.     def __init__(self, email = None, password = None, source = None, server = 'www.google.com', **kwargs):
  35.         """Creates a client for the Google Webmaster Tools service.
  36.  
  37.     Args:
  38.       email: string (optional) The user's email address, used for
  39.           authentication.
  40.       password: string (optional) The user's password.
  41.       source: string (optional) The name of the user's application.
  42.       server: string (optional) The name of the server to which a connection
  43.           will be opened. Default value: 'www.google.com'.
  44.       **kwargs: The other parameters to pass to gdata.service.GDataService
  45.           constructor.
  46.     """
  47.         gdata.service.GDataService.__init__(self, email = email, password = password, service = 'sitemaps', source = source, server = server, **kwargs)
  48.  
  49.     
  50.     def GetSitesFeed(self, uri = SITES_FEED, converter = webmastertools.SitesFeedFromString):
  51.         """Gets sites feed.
  52.  
  53.     Args:
  54.       uri: str (optional) URI to retrieve sites feed.
  55.       converter: func (optional) Function which is executed on the server's
  56.           response before it is returned. Usually this is a function like
  57.           SitesFeedFromString which will parse the response and turn it into
  58.           an object.
  59.  
  60.     Returns:
  61.       If converter is defined, the results of running converter on the server's
  62.       response. Otherwise, it will be a SitesFeed object.
  63.     """
  64.         return self.Get(uri, converter = converter)
  65.  
  66.     
  67.     def AddSite(self, site_uri, uri = SITES_FEED, url_params = None, escape_params = True, converter = None):
  68.         """Adds a site to Google Webmaster Tools.
  69.  
  70.     Args: 
  71.       site_uri: str URI of which site to add.
  72.       uri: str (optional) URI to add a site.
  73.       url_params: dict (optional) Additional URL parameters to be included
  74.                   in the insertion request. 
  75.       escape_params: boolean (optional) If true, the url_parameters will be
  76.                      escaped before they are included in the request.
  77.       converter: func (optional) Function which is executed on the server's
  78.           response before it is returned. Usually this is a function like
  79.           SitesEntryFromString which will parse the response and turn it into
  80.           an object.
  81.  
  82.     Returns:
  83.       If converter is defined, the results of running converter on the server's
  84.       response. Otherwise, it will be a SitesEntry object.
  85.     """
  86.         site_entry = webmastertools.SitesEntry()
  87.         site_entry.content = atom.Content(src = site_uri)
  88.         response = self.Post(site_entry, uri, url_params = url_params, escape_params = escape_params, converter = converter)
  89.         if not converter and isinstance(response, atom.Entry):
  90.             return webmastertools.SitesEntryFromString(response.ToString())
  91.         return response
  92.  
  93.     
  94.     def DeleteSite(self, site_uri, uri = SITE_TEMPLATE, url_params = None, escape_params = True):
  95.         '''Removes a site from Google Webmaster Tools.
  96.  
  97.     Args: 
  98.       site_uri: str URI of which site to remove.
  99.       uri: str (optional) A URI template to send DELETE request.
  100.            Default SITE_TEMPLATE.
  101.       url_params: dict (optional) Additional URL parameters to be included
  102.                   in the insertion request. 
  103.       escape_params: boolean (optional) If true, the url_parameters will be
  104.                      escaped before they are included in the request.
  105.  
  106.     Returns:
  107.       True if the delete succeeded.
  108.     '''
  109.         return self.Delete(uri % urllib.quote_plus(site_uri), url_params = url_params, escape_params = escape_params)
  110.  
  111.     
  112.     def VerifySite(self, site_uri, verification_method, uri = SITE_TEMPLATE, url_params = None, escape_params = True, converter = None):
  113.         """Requests a verification of a site.
  114.  
  115.     Args: 
  116.       site_uri: str URI of which site to add sitemap for.
  117.       verification_method: str The method to verify a site. Valid values are
  118.                            'htmlpage', and 'metatag'.
  119.       uri: str (optional) URI template to update a site.
  120.            Default SITE_TEMPLATE.
  121.       url_params: dict (optional) Additional URL parameters to be included
  122.                   in the insertion request. 
  123.       escape_params: boolean (optional) If true, the url_parameters will be
  124.                      escaped before they are included in the request.
  125.       converter: func (optional) Function which is executed on the server's
  126.           response before it is returned. Usually this is a function like
  127.           SitemapsEntryFromString which will parse the response and turn it into
  128.           an object.
  129.  
  130.     Returns:
  131.       If converter is defined, the results of running converter on the server's
  132.       response. Otherwise, it will be a SitesEntry object.
  133.     """
  134.         site_entry = webmastertools.SitesEntry(atom_id = atom.Id(text = site_uri), category = atom.Category(scheme = 'http://schemas.google.com/g/2005#kind', term = 'http://schemas.google.com/webmasters/tools/2007#sites-info'), verification_method = webmastertools.VerificationMethod(type = verification_method, in_user = 'true'))
  135.         response = self.Put(site_entry, uri % urllib.quote_plus(site_uri), url_params = url_params, escape_params = escape_params, converter = converter)
  136.         if not converter and isinstance(response, atom.Entry):
  137.             return webmastertools.SitesEntryFromString(response.ToString())
  138.         return response
  139.  
  140.     
  141.     def UpdateGeoLocation(self, site_uri, geolocation, uri = SITE_TEMPLATE, url_params = None, escape_params = True, converter = None):
  142.         """Updates geolocation setting of a site.
  143.  
  144.     Args: 
  145.       site_uri: str URI of which site to add sitemap for.
  146.       geolocation: str The geographic location. Valid values are listed in
  147.                    http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
  148.       uri: str (optional) URI template to update a site.
  149.            Default SITE_TEMPLATE.
  150.       url_params: dict (optional) Additional URL parameters to be included
  151.                   in the insertion request. 
  152.       escape_params: boolean (optional) If true, the url_parameters will be
  153.                      escaped before they are included in the request.
  154.       converter: func (optional) Function which is executed on the server's
  155.           response before it is returned. Usually this is a function like
  156.           SitemapsEntryFromString which will parse the response and turn it into
  157.           an object.
  158.  
  159.     Returns:
  160.       If converter is defined, the results of running converter on the server's
  161.       response. Otherwise, it will be a SitesEntry object.
  162.     """
  163.         site_entry = webmastertools.SitesEntry(atom_id = atom.Id(text = site_uri), category = atom.Category(scheme = 'http://schemas.google.com/g/2005#kind', term = 'http://schemas.google.com/webmasters/tools/2007#sites-info'), geolocation = webmastertools.GeoLocation(text = geolocation))
  164.         response = self.Put(site_entry, uri % urllib.quote_plus(site_uri), url_params = url_params, escape_params = escape_params, converter = converter)
  165.         if not converter and isinstance(response, atom.Entry):
  166.             return webmastertools.SitesEntryFromString(response.ToString())
  167.         return response
  168.  
  169.     
  170.     def UpdateCrawlRate(self, site_uri, crawl_rate, uri = SITE_TEMPLATE, url_params = None, escape_params = True, converter = None):
  171.         """Updates crawl rate setting of a site.
  172.  
  173.     Args: 
  174.       site_uri: str URI of which site to add sitemap for.
  175.       crawl_rate: str The crawl rate for a site. Valid values are 'slower',
  176.                   'normal', and 'faster'.
  177.       uri: str (optional) URI template to update a site.
  178.            Default SITE_TEMPLATE.
  179.       url_params: dict (optional) Additional URL parameters to be included
  180.                   in the insertion request. 
  181.       escape_params: boolean (optional) If true, the url_parameters will be
  182.                      escaped before they are included in the request.
  183.       converter: func (optional) Function which is executed on the server's
  184.           response before it is returned. Usually this is a function like
  185.           SitemapsEntryFromString which will parse the response and turn it into
  186.           an object.
  187.  
  188.     Returns:
  189.       If converter is defined, the results of running converter on the server's
  190.       response. Otherwise, it will be a SitesEntry object.
  191.     """
  192.         site_entry = webmastertools.SitesEntry(atom_id = atom.Id(text = site_uri), category = atom.Category(scheme = 'http://schemas.google.com/g/2005#kind', term = 'http://schemas.google.com/webmasters/tools/2007#sites-info'), crawl_rate = webmastertools.CrawlRate(text = crawl_rate))
  193.         response = self.Put(site_entry, uri % urllib.quote_plus(site_uri), url_params = url_params, escape_params = escape_params, converter = converter)
  194.         if not converter and isinstance(response, atom.Entry):
  195.             return webmastertools.SitesEntryFromString(response.ToString())
  196.         return response
  197.  
  198.     
  199.     def UpdatePreferredDomain(self, site_uri, preferred_domain, uri = SITE_TEMPLATE, url_params = None, escape_params = True, converter = None):
  200.         """Updates preferred domain setting of a site.
  201.  
  202.     Note that if using 'preferwww', will also need www.example.com in account to
  203.     take effect.
  204.  
  205.     Args: 
  206.       site_uri: str URI of which site to add sitemap for.
  207.       preferred_domain: str The preferred domain for a site. Valid values are 'none',
  208.                         'preferwww', and 'prefernowww'.
  209.       uri: str (optional) URI template to update a site.
  210.            Default SITE_TEMPLATE.
  211.       url_params: dict (optional) Additional URL parameters to be included
  212.                   in the insertion request. 
  213.       escape_params: boolean (optional) If true, the url_parameters will be
  214.                      escaped before they are included in the request.
  215.       converter: func (optional) Function which is executed on the server's
  216.           response before it is returned. Usually this is a function like
  217.           SitemapsEntryFromString which will parse the response and turn it into
  218.           an object.
  219.  
  220.     Returns:
  221.       If converter is defined, the results of running converter on the server's
  222.       response. Otherwise, it will be a SitesEntry object.
  223.     """
  224.         site_entry = webmastertools.SitesEntry(atom_id = atom.Id(text = site_uri), category = atom.Category(scheme = 'http://schemas.google.com/g/2005#kind', term = 'http://schemas.google.com/webmasters/tools/2007#sites-info'), preferred_domain = webmastertools.PreferredDomain(text = preferred_domain))
  225.         response = self.Put(site_entry, uri % urllib.quote_plus(site_uri), url_params = url_params, escape_params = escape_params, converter = converter)
  226.         if not converter and isinstance(response, atom.Entry):
  227.             return webmastertools.SitesEntryFromString(response.ToString())
  228.         return response
  229.  
  230.     
  231.     def UpdateEnhancedImageSearch(self, site_uri, enhanced_image_search, uri = SITE_TEMPLATE, url_params = None, escape_params = True, converter = None):
  232.         """Updates enhanced image search setting of a site.
  233.  
  234.     Args: 
  235.       site_uri: str URI of which site to add sitemap for.
  236.       enhanced_image_search: str The enhanced image search setting for a site.
  237.                              Valid values are 'true', and 'false'.
  238.       uri: str (optional) URI template to update a site.
  239.            Default SITE_TEMPLATE.
  240.       url_params: dict (optional) Additional URL parameters to be included
  241.                   in the insertion request. 
  242.       escape_params: boolean (optional) If true, the url_parameters will be
  243.                      escaped before they are included in the request.
  244.       converter: func (optional) Function which is executed on the server's
  245.           response before it is returned. Usually this is a function like
  246.           SitemapsEntryFromString which will parse the response and turn it into
  247.           an object.
  248.  
  249.     Returns:
  250.       If converter is defined, the results of running converter on the server's
  251.       response. Otherwise, it will be a SitesEntry object.
  252.     """
  253.         site_entry = webmastertools.SitesEntry(atom_id = atom.Id(text = site_uri), category = atom.Category(scheme = 'http://schemas.google.com/g/2005#kind', term = 'http://schemas.google.com/webmasters/tools/2007#sites-info'), enhanced_image_search = webmastertools.EnhancedImageSearch(text = enhanced_image_search))
  254.         response = self.Put(site_entry, uri % urllib.quote_plus(site_uri), url_params = url_params, escape_params = escape_params, converter = converter)
  255.         if not converter and isinstance(response, atom.Entry):
  256.             return webmastertools.SitesEntryFromString(response.ToString())
  257.         return response
  258.  
  259.     
  260.     def GetSitemapsFeed(self, site_uri, uri = SITEMAPS_FEED_TEMPLATE, converter = webmastertools.SitemapsFeedFromString):
  261.         """Gets sitemaps feed of a site.
  262.     
  263.     Args:
  264.       site_uri: str (optional) URI of which site to retrieve its sitemaps feed.
  265.       uri: str (optional) URI to retrieve sites feed.
  266.       converter: func (optional) Function which is executed on the server's
  267.           response before it is returned. Usually this is a function like
  268.           SitemapsFeedFromString which will parse the response and turn it into
  269.           an object.
  270.  
  271.     Returns:
  272.       If converter is defined, the results of running converter on the server's
  273.       response. Otherwise, it will be a SitemapsFeed object.
  274.     """
  275.         return self.Get(uri % {
  276.             'site_id': urllib.quote_plus(site_uri) }, converter = converter)
  277.  
  278.     
  279.     def AddSitemap(self, site_uri, sitemap_uri, sitemap_type = 'WEB', uri = SITEMAPS_FEED_TEMPLATE, url_params = None, escape_params = True, converter = None):
  280.         """Adds a regular sitemap to a site.
  281.  
  282.     Args: 
  283.       site_uri: str URI of which site to add sitemap for.
  284.       sitemap_uri: str URI of sitemap to add to a site.
  285.       sitemap_type: str Type of added sitemap. Valid types: WEB, VIDEO, or CODE.
  286.       uri: str (optional) URI template to add a sitemap.
  287.            Default SITEMAP_FEED_TEMPLATE.
  288.       url_params: dict (optional) Additional URL parameters to be included
  289.                   in the insertion request. 
  290.       escape_params: boolean (optional) If true, the url_parameters will be
  291.                      escaped before they are included in the request.
  292.       converter: func (optional) Function which is executed on the server's
  293.           response before it is returned. Usually this is a function like
  294.           SitemapsEntryFromString which will parse the response and turn it into
  295.           an object.
  296.  
  297.     Returns:
  298.       If converter is defined, the results of running converter on the server's
  299.       response. Otherwise, it will be a SitemapsEntry object.
  300.     """
  301.         sitemap_entry = webmastertools.SitemapsEntry(atom_id = atom.Id(text = sitemap_uri), category = atom.Category(scheme = 'http://schemas.google.com/g/2005#kind', term = 'http://schemas.google.com/webmasters/tools/2007#sitemap-regular'), sitemap_type = webmastertools.SitemapType(text = sitemap_type))
  302.         response = self.Post(sitemap_entry, uri % {
  303.             'site_id': urllib.quote_plus(site_uri) }, url_params = url_params, escape_params = escape_params, converter = converter)
  304.         if not converter and isinstance(response, atom.Entry):
  305.             return webmastertools.SitemapsEntryFromString(response.ToString())
  306.         return response
  307.  
  308.     
  309.     def AddMobileSitemap(self, site_uri, sitemap_uri, sitemap_mobile_markup_language = 'XHTML', uri = SITEMAPS_FEED_TEMPLATE, url_params = None, escape_params = True, converter = None):
  310.         """Adds a mobile sitemap to a site.
  311.  
  312.     Args: 
  313.       site_uri: str URI of which site to add sitemap for.
  314.       sitemap_uri: str URI of sitemap to add to a site.
  315.       sitemap_mobile_markup_language: str Format of added sitemap. Valid types:
  316.                                       XHTML, WML, or cHTML.
  317.       uri: str (optional) URI template to add a sitemap.
  318.            Default SITEMAP_FEED_TEMPLATE.
  319.       url_params: dict (optional) Additional URL parameters to be included
  320.                   in the insertion request. 
  321.       escape_params: boolean (optional) If true, the url_parameters will be
  322.                      escaped before they are included in the request.
  323.       converter: func (optional) Function which is executed on the server's
  324.           response before it is returned. Usually this is a function like
  325.           SitemapsEntryFromString which will parse the response and turn it into
  326.           an object.
  327.  
  328.     Returns:
  329.       If converter is defined, the results of running converter on the server's
  330.       response. Otherwise, it will be a SitemapsEntry object.
  331.     """
  332.         sitemap_entry = webmastertools.SitemapsEntry(atom_id = atom.Id(text = sitemap_uri), category = atom.Category(scheme = 'http://schemas.google.com/g/2005#kind', term = 'http://schemas.google.com/webmasters/tools/2007#sitemap-mobile'), sitemap_mobile_markup_language = webmastertools.SitemapMobileMarkupLanguage(text = sitemap_mobile_markup_language))
  333.         print sitemap_entry
  334.         response = self.Post(sitemap_entry, uri % {
  335.             'site_id': urllib.quote_plus(site_uri) }, url_params = url_params, escape_params = escape_params, converter = converter)
  336.         if not converter and isinstance(response, atom.Entry):
  337.             return webmastertools.SitemapsEntryFromString(response.ToString())
  338.         return response
  339.  
  340.     
  341.     def AddNewsSitemap(self, site_uri, sitemap_uri, sitemap_news_publication_label, uri = SITEMAPS_FEED_TEMPLATE, url_params = None, escape_params = True, converter = None):
  342.         """Adds a news sitemap to a site.
  343.  
  344.     Args: 
  345.       site_uri: str URI of which site to add sitemap for.
  346.       sitemap_uri: str URI of sitemap to add to a site.
  347.       sitemap_news_publication_label: str, list of str Publication Labels for
  348.                                       sitemap.
  349.       uri: str (optional) URI template to add a sitemap.
  350.            Default SITEMAP_FEED_TEMPLATE.
  351.       url_params: dict (optional) Additional URL parameters to be included
  352.                   in the insertion request. 
  353.       escape_params: boolean (optional) If true, the url_parameters will be
  354.                      escaped before they are included in the request.
  355.       converter: func (optional) Function which is executed on the server's
  356.           response before it is returned. Usually this is a function like
  357.           SitemapsEntryFromString which will parse the response and turn it into
  358.           an object.
  359.  
  360.     Returns:
  361.       If converter is defined, the results of running converter on the server's
  362.       response. Otherwise, it will be a SitemapsEntry object.
  363.     """
  364.         sitemap_entry = webmastertools.SitemapsEntry(atom_id = atom.Id(text = sitemap_uri), category = atom.Category(scheme = 'http://schemas.google.com/g/2005#kind', term = 'http://schemas.google.com/webmasters/tools/2007#sitemap-news'), sitemap_news_publication_label = [])
  365.         if isinstance(sitemap_news_publication_label, str):
  366.             sitemap_news_publication_label = [
  367.                 sitemap_news_publication_label]
  368.         
  369.         for label in sitemap_news_publication_label:
  370.             sitemap_entry.sitemap_news_publication_label.append(webmastertools.SitemapNewsPublicationLabel(text = label))
  371.         
  372.         print sitemap_entry
  373.         response = self.Post(sitemap_entry, uri % {
  374.             'site_id': urllib.quote_plus(site_uri) }, url_params = url_params, escape_params = escape_params, converter = converter)
  375.         if not converter and isinstance(response, atom.Entry):
  376.             return webmastertools.SitemapsEntryFromString(response.ToString())
  377.         return response
  378.  
  379.     
  380.     def DeleteSitemap(self, site_uri, sitemap_uri, uri = SITEMAP_TEMPLATE, url_params = None, escape_params = True):
  381.         '''Removes a sitemap from a site.
  382.  
  383.     Args: 
  384.       site_uri: str URI of which site to remove a sitemap from.
  385.       sitemap_uri: str URI of sitemap to remove from a site.
  386.       uri: str (optional) A URI template to send DELETE request.
  387.            Default SITEMAP_TEMPLATE.
  388.       url_params: dict (optional) Additional URL parameters to be included
  389.                   in the insertion request. 
  390.       escape_params: boolean (optional) If true, the url_parameters will be
  391.                      escaped before they are included in the request.
  392.  
  393.     Returns:
  394.       True if the delete succeeded.
  395.     '''
  396.         return self.Delete(uri % {
  397.             'site_id': urllib.quote_plus(site_uri),
  398.             'sitemap_id': urllib.quote_plus(sitemap_uri) }, url_params = url_params, escape_params = escape_params)
  399.  
  400.  
  401.